package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.bean.vo.sys.req.UpdateMonitorConfigDetailReqParam;
import cn.com.cifi.mars.bean.vo.sys.rsp.MonitorConfigDetailVo;
import cn.com.cifi.mars.bean.vo.sys.rsp.MonitorDetailVo;
import cn.com.cifi.mars.common.exception.MsgException;
import cn.com.cifi.mars.constant.CodeEnum;
import cn.com.cifi.mars.constant.ConditionTypeEnum;
import cn.com.cifi.mars.constant.Constants;
import cn.com.cifi.mars.constant.IsDeleteEnum;
import cn.com.cifi.mars.entity.MonitorConfig;
import cn.com.cifi.mars.entity.MonitorConfigDetail;
import cn.com.cifi.mars.mapper.mars.MonitorConfigDetailMapper;
import cn.com.cifi.mars.mapper.mars.MonitorConfigMapper;
import cn.com.cifi.mars.service.MonitorConfigDetailService;
import cn.com.cifi.mars.util.StrUtils;
import cn.com.cifi.mars.util.UUIDUtils;
import io.swagger.annotations.ApiModelProperty;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 监控设置明细 服务实现类
 * </p>
 *
 * @author plx
 * @since 2020-02-05
 */
@Service
public class MonitorConfigDetailServiceImpl extends ServiceImpl<MonitorConfigDetailMapper, MonitorConfigDetail> implements MonitorConfigDetailService {

	@Autowired
	private MonitorConfigDetailMapper monitorConfigDetailMapper;
	@Autowired
	private MonitorConfigMapper monitorConfigMapper;
	
	/**
	 * 根据组织机构id,查询监控标准信息
	 */
	@Override
	public List<MonitorConfigDetailVo> getMonitorConfigDetailList(String orgId) {
		List<MonitorConfigDetailVo>detailList=new ArrayList<>();
		List<MonitorConfigDetailVo>list=monitorConfigDetailMapper.selectMonitorConfigDetailList(orgId,IsDeleteEnum.NO.getKey()+"");
		for (MonitorConfigDetailVo md : list) {
			
			if(StrUtils.isEmpty(md.getD1MonitorDetailId())) {
				//查询旭辉集团的数据
				md=monitorConfigDetailMapper.selectByMonitorIdAndOrgId(md.getMonitorId(), IsDeleteEnum.NO.getKey()+"", Constants.XUHUI_GROUP);
			}
			
			if(StringUtils.isEmpty(md.getD1MinValue1())) {
				md.setD1MinValue1("");
			}
			if(StringUtils.isEmpty(md.getD2MinValue1())) {
				md.setD2MinValue1("");
			}
			if(StringUtils.isEmpty(md.getD3MinValue1())) {
				md.setD3MinValue1("");
			}
			
			if(StringUtils.isEmpty(md.getD1MinValue2())) {
				md.setD1MinValue2("");
			}
			if(StringUtils.isEmpty(md.getD2MinValue2())) {
				md.setD2MinValue2("");
			}
			if(StringUtils.isEmpty(md.getD3MinValue2())) {
				md.setD3MinValue2("");
			}
			
			if(StringUtils.isEmpty(md.getD1MaxValue1())) {
				md.setD1MaxValue1("");
			}
			if(StringUtils.isEmpty(md.getD2MaxValue1())) {
				md.setD2MaxValue1("");
			}
			if(StringUtils.isEmpty(md.getD3MaxValue1())) {
				md.setD3MaxValue1("");
			}
			
			if(StringUtils.isEmpty(md.getD1MaxValue2())) {
				md.setD1MaxValue2("");
			}
			if(StringUtils.isEmpty(md.getD2MaxValue2())) {
				md.setD2MaxValue2("");
			}
			if(StringUtils.isEmpty(md.getD3MaxValue2())) {
				md.setD3MaxValue2("");
			}
			
			//2.0≤指标<2.5 或  3.5<指标<9.0
			//健康标准内容
			String healthStandard=md.getD1MinValue1()+StrUtils.getOperator(md.getD1Operator1())
			+Constants.INDEX_NAME+StrUtils.getOperator(md.getD1Operator2())+md.getD1MaxValue1();
			//提醒标准内容
			String remindStandard=md.getD2MinValue1()+StrUtils.getOperator(md.getD2Operator1())
			+Constants.INDEX_NAME+StrUtils.getOperator(md.getD2Operator2())+md.getD2MaxValue1();
			//预警标准内容
			String earlyWarningStandard=md.getD3MinValue1()+StrUtils.getOperator(md.getD3Operator1())
			+Constants.INDEX_NAME+StrUtils.getOperator(md.getD3Operator2())+md.getD3MaxValue1();
		
			//条件类型(0默认 1或 2且)
			if(md.getD1ConditionType().equals(ConditionTypeEnum.ONE.getKey())){
				healthStandard=healthStandard+" "+ConditionTypeEnum.ONE.getValue()
				+" "+md.getD1MinValue2()+ StrUtils.getOperator(md.getD1Operator3())
				+Constants.INDEX_NAME+ StrUtils.getOperator(md.getD1Operator4())
				+md.getD1MaxValue2();
			}else if(md.getD1ConditionType().equals(ConditionTypeEnum.TWO.getKey())){
				healthStandard=healthStandard+" "+ConditionTypeEnum.TWO.getValue()
				+" "+md.getD1MinValue2()+ StrUtils.getOperator(md.getD1Operator3())
				+Constants.INDEX_NAME+ StrUtils.getOperator(md.getD1Operator4())
				+md.getD1MaxValue2();
			}
			
			if(md.getD2ConditionType().equals(ConditionTypeEnum.ONE.getKey())){
				remindStandard=remindStandard+" "+ConditionTypeEnum.ONE.getValue()
				+" "+md.getD2MinValue2()+ StrUtils.getOperator(md.getD2Operator3())
				+Constants.INDEX_NAME+StrUtils.getOperator(md.getD2Operator4())
						+md.getD2MaxValue2();
			}else if(md.getD2ConditionType().equals(ConditionTypeEnum.TWO.getKey())){
				remindStandard=remindStandard+" "+ConditionTypeEnum.TWO.getValue()
				+" "+md.getD2MinValue2()+StrUtils.getOperator(md.getD2Operator3())
				+Constants.INDEX_NAME+StrUtils.getOperator(md.getD2Operator4())
						+md.getD2MaxValue2();
			}
			
			if(md.getD3ConditionType().equals(ConditionTypeEnum.ONE.getKey())){
				earlyWarningStandard=earlyWarningStandard+" "+ConditionTypeEnum.ONE.getValue()
				+" "+md.getD3MinValue2()+StrUtils.getOperator(md.getD3Operator3())
				+Constants.INDEX_NAME+StrUtils.getOperator(md.getD3Operator4())+
				md.getD3MaxValue2();
			}else if(md.getD3ConditionType().equals(ConditionTypeEnum.TWO.getKey())){
				earlyWarningStandard=earlyWarningStandard+" "+ConditionTypeEnum.TWO.getValue()
				+" "+md.getD3MinValue2()+StrUtils.getOperator(md.getD3Operator3())
				+Constants.INDEX_NAME+StrUtils.getOperator(md.getD3Operator4())
						+md.getD3MaxValue2();
			}
			
			md.setHealthStandard(healthStandard);
			md.setRemindStandard(remindStandard);
			md.setEarlyWarningStandard(earlyWarningStandard);
			detailList.add(md);
		}
		return detailList;
	}

	/**
	 * 编辑监控指标标准
	 */
	@Override
	public String updateMonitorConfigDetail(UpdateMonitorConfigDetailReqParam param) {
		//获取监控详情表是否存在
		MonitorConfigDetail updM1=monitorConfigDetailMapper.selectByIdAndIsDelete(param.getD1MonitorDetailId(),  IsDeleteEnum.NO.getKey()+"");
		MonitorConfigDetail updM2=monitorConfigDetailMapper.selectByIdAndIsDelete(param.getD2MonitorDetailId(),  IsDeleteEnum.NO.getKey()+"");
		MonitorConfigDetail updM3=monitorConfigDetailMapper.selectByIdAndIsDelete(param.getD3MonitorDetailId(),  IsDeleteEnum.NO.getKey()+"");
		if(null==updM1) {
			throw new MsgException(CodeEnum.MONITOR_ERROR);
		}
		if(null==updM2) {
			throw new MsgException(CodeEnum.MONITOR_ERROR);	
		}
		if(null==updM3) {
			throw new MsgException(CodeEnum.MONITOR_ERROR);
		}
		if(StringUtils.isEmpty(param.getMonitorId())) {
			throw new MsgException(CodeEnum.MONITOR_ERROR);
		}
		
		//根据组织机构id和监控标准详情id查询是否存在
		MonitorConfigDetail m1=monitorConfigDetailMapper.selectByIdAndOrgIdAndIsDelete(param.getD1MonitorDetailId(), param.getOrgId(),IsDeleteEnum.NO.getKey()+"");
		MonitorConfigDetail m2=monitorConfigDetailMapper.selectByIdAndOrgIdAndIsDelete(param.getD2MonitorDetailId(), param.getOrgId(),IsDeleteEnum.NO.getKey()+"");
		MonitorConfigDetail m3=monitorConfigDetailMapper.selectByIdAndOrgIdAndIsDelete(param.getD3MonitorDetailId(), param.getOrgId(),IsDeleteEnum.NO.getKey()+"");
		if(null==m1) {
			//新增
			m1=new MonitorConfigDetail();
			m1=coverDetail1(param, m1);
			m1.setId(UUIDUtils.create());
			m1.setMonitorId(param.getMonitorId());
			//监控设置明细类型(1健康标准2提醒标准3预警标准)
			m1.setMonitorType(Constants.ONE+"");
			
			m2=new MonitorConfigDetail();
			m2=coverDetail2(param, m2);
			m2.setId(UUIDUtils.create());
			m2.setMonitorId(param.getMonitorId());
			m2.setMonitorType(Constants.TWO+"");
			
			m3=new MonitorConfigDetail();
			m3=coverDetail3(param, m3);
			m3.setId(UUIDUtils.create());
			m3.setMonitorId(param.getMonitorId());
			m3.setMonitorType(Constants.THREE+"");
			
			int msg1=monitorConfigDetailMapper.insert(m1);
			int msg2=monitorConfigDetailMapper.insert(m2);
			int msg3=monitorConfigDetailMapper.insert(m3);
			return msg3+"";
		}else {
			//修改
			m1=coverDetail1(param, m1);
			m2=coverDetail2(param, m2);
			m3=coverDetail3(param, m3);
			monitorConfigDetailMapper.updateByMonitorDetailId(m1);
			monitorConfigDetailMapper.updateByMonitorDetailId(m2);
			int msg=monitorConfigDetailMapper.updateByMonitorDetailId(m3);
			return msg+"";
		}
	}

	
	/**
	 * 初始化detail数据
	 */
	public void initDetail() {
		QueryWrapper<MonitorConfig> queryWrapper=new QueryWrapper<>();
		//查询配置表
		List<MonitorConfig>list=monitorConfigMapper.selectList(queryWrapper);
		for (MonitorConfig mc : list) {
			String orgId=Constants.XUHUI_GROUP;
			MonitorConfigDetail m1=new MonitorConfigDetail();
			MonitorConfigDetail m2=new MonitorConfigDetail();
			MonitorConfigDetail m3=new MonitorConfigDetail();
			//条件类型(0默认 1或 2且) 【另：该字段可做排序使用】
			m1.setConditionType(ConditionTypeEnum.ZERO.getKey());
			m2.setConditionType(ConditionTypeEnum.ZERO.getKey());
			m3.setConditionType(ConditionTypeEnum.ZERO.getKey());
			m1.setMinValue1(Constants.MIN);
			m1.setMinValue2(Constants.MIN);
			m1.setMaxValue1(Constants.MAX);
			m1.setMaxValue2(Constants.MAX);
			m2.setMinValue1(Constants.MIN);
			m2.setMinValue2(Constants.MIN);
			m2.setMaxValue1(Constants.MAX);
			m2.setMaxValue2(Constants.MAX);
			m3.setMinValue1(Constants.MIN);
			m3.setMinValue2(Constants.MIN);
			m3.setMaxValue1(Constants.MAX);
			m3.setMaxValue2(Constants.MAX);
			m1.setOperator1(Constants.TWO+"");
			m1.setOperator2(Constants.TWO+"");
			m1.setOperator3(Constants.TWO+"");
			m1.setOperator4(Constants.TWO+"");
			
			m2.setOperator1(Constants.TWO+"");
			m2.setOperator2(Constants.TWO+"");
			m2.setOperator3(Constants.TWO+"");
			m2.setOperator4(Constants.TWO+"");
			
			m3.setOperator1(Constants.TWO+"");
			m3.setOperator2(Constants.TWO+"");
			m3.setOperator3(Constants.TWO+"");
			m3.setOperator4(Constants.TWO+"");
			m1.setOrgid(orgId);
			m2.setOrgid(orgId);
			m3.setOrgid(orgId);
			m1.setUpdatetime(LocalDateTime.now());
			m2.setUpdatetime(LocalDateTime.now());
			m3.setUpdatetime(LocalDateTime.now());
			//监控设置明细类型(1健康标准2提醒标准3预警标准)
			m1.setMonitorType(Constants.ONE+"");
			m2.setMonitorType(Constants.TWO+"");
			m3.setMonitorType(Constants.THREE+"");
			m1.setMonitorId(mc.getId());
			m2.setMonitorId(mc.getId());
			m3.setMonitorId(mc.getId());
			m1.setIsdelete(IsDeleteEnum.NO.getKey()+"");
			m2.setIsdelete(IsDeleteEnum.NO.getKey()+"");
			m3.setIsdelete(IsDeleteEnum.NO.getKey()+"");
			m1.setId(UUIDUtils.create());
			m2.setId(UUIDUtils.create());
			m3.setId(UUIDUtils.create());
			
			monitorConfigDetailMapper.insert(m1);
			monitorConfigDetailMapper.insert(m2);
			monitorConfigDetailMapper.insert(m3);
		}
	}
	
	/**
	 * 根据监控详情id和组织机构id,查询监控标准详情
	 */
	@Override
	public MonitorDetailVo getMonitorConfigDetail(String orgId, String d1MonitorDetailId, String d2MonitorDetailId,
			String d3MonitorDetailId) {
		MonitorDetailVo vo=new MonitorDetailVo();
		MonitorConfigDetail m1=monitorConfigDetailMapper.selectByIdAndIsDelete(d1MonitorDetailId, IsDeleteEnum.NO.getKey()+"");
		MonitorConfigDetail m2=monitorConfigDetailMapper.selectByIdAndIsDelete(d2MonitorDetailId, IsDeleteEnum.NO.getKey()+"");
		MonitorConfigDetail m3=monitorConfigDetailMapper.selectByIdAndIsDelete(d3MonitorDetailId, IsDeleteEnum.NO.getKey()+"");
		if(null==m1) {
			throw new MsgException(CodeEnum.MONITOR_ERROR);
		}
		if(null==m2) {
			throw new MsgException(CodeEnum.MONITOR_ERROR);	
		}
		if(null==m3) {
			throw new MsgException(CodeEnum.MONITOR_ERROR);
		}
		//条件类型(0默认 1或 2且)
		vo.setD1ConditionType(m1.getConditionType());
		vo.setD2ConditionType(m2.getConditionType());
		vo.setD3ConditionType(m3.getConditionType());
		//最小值1
		vo.setD1MinValue1(m1.getMinValue1()+"");
		vo.setD2MinValue1(m2.getMinValue1()+"");
		vo.setD3MinValue1(m3.getMinValue1()+"");
		
		if(null==m1.getMinValue1()) {
			vo.setD1MinValue1("");
		}
		if(null==m2.getMinValue1()) {
			vo.setD2MinValue1("");
		}
		if(null==m3.getMinValue1()) {
			vo.setD3MinValue1("");
		}
		
		//最小值2
		vo.setD1MinValue2(m1.getMinValue2()+"");
		vo.setD2MinValue2(m2.getMinValue2()+"");
		vo.setD3MinValue2(m3.getMinValue2()+"");
		
		if(null==m1.getMinValue2()) {
			vo.setD1MinValue2("");
		}
		if(null==m2.getMinValue2()) {
			vo.setD2MinValue2("");
		}
		if(null==m3.getMinValue2()) {
			vo.setD3MinValue2("");
		}
		//最大值1
		vo.setD1MaxValue1(m1.getMaxValue1()+"");
		vo.setD2MaxValue1(m2.getMaxValue1()+"");
		vo.setD3MaxValue1(m3.getMaxValue1()+"");
		
		if(null==m1.getMaxValue1()) {
			vo.setD1MaxValue1("");
		}
		if(null==m2.getMaxValue1()) {
			vo.setD2MaxValue1("");
		}
		if(null==m3.getMaxValue1()) {
			vo.setD3MaxValue1("");
		}
		//最大值2
		vo.setD1MaxValue2(m1.getMaxValue2()+"");
		vo.setD2MaxValue2(m2.getMaxValue2()+"");
		vo.setD3MaxValue2(m3.getMaxValue2()+"");
		
		if(null==m1.getMaxValue2()) {
			vo.setD1MaxValue2("");
		}
		if(null==m2.getMaxValue2()) {
			vo.setD2MaxValue2("");
		}
		if(null==m3.getMaxValue2()) {
			vo.setD3MaxValue2("");
		}
		
		//运算符1(1无|2小于<|3小于等于≤)
		vo.setD1Operator1(m1.getOperator1());
		vo.setD1Operator2(m1.getOperator2());
		vo.setD1Operator3(m1.getOperator3());
		vo.setD1Operator4(m1.getOperator4());
		
		vo.setD2Operator1(m2.getOperator1());
		vo.setD2Operator2(m2.getOperator2());
		vo.setD2Operator3(m2.getOperator3());
		vo.setD2Operator4(m2.getOperator4());
		
		vo.setD3Operator1(m3.getOperator1());
		vo.setD3Operator2(m3.getOperator2());
		vo.setD3Operator3(m3.getOperator3());
		vo.setD3Operator4(m3.getOperator4());
		
		vo.setD1MonitorDetailId(d1MonitorDetailId);
		vo.setD2MonitorDetailId(d2MonitorDetailId);
		vo.setD3MonitorDetailId(d3MonitorDetailId);
		vo.setMonitorId(m1.getMonitorId());
		vo.setOrgId(orgId);
		return vo;
	}
	
	/**
	 * 覆盖原有数据
	 * @param param
	 * @param m1
	 * @return
	 */
	public MonitorConfigDetail coverDetail1(UpdateMonitorConfigDetailReqParam param,MonitorConfigDetail m1) {
		m1.setConditionType(param.getD1ConditionType());
		m1.setOrgid(param.getOrgId());
		m1.setUpdatetime(LocalDateTime.now());
		if(StringUtils.isNotBlank(param.getD1MinValue1())) {
			m1.setMinValue1(Double.parseDouble(param.getD1MinValue1()));
		}else {
			m1.setMinValue1(null);
		}
		if(StringUtils.isNotBlank(param.getD1MinValue2())) {
			m1.setMinValue2(Double.parseDouble(param.getD1MinValue2()));	
		}else {
			m1.setMinValue2(null);
		}
		if(StringUtils.isNotBlank(param.getD1MaxValue1())) {
			m1.setMaxValue1(Double.parseDouble(param.getD1MaxValue1()));
		}else {
			m1.setMaxValue1(null);
		}
		if(StringUtils.isNotBlank(param.getD1MaxValue2())) {
			m1.setMaxValue2(Double.parseDouble(param.getD1MaxValue2()));
		}else {
			m1.setMaxValue2(null);
		}
		m1.setOperator1(param.getD1Operator1());
		m1.setOperator2(param.getD1Operator2());
		m1.setOperator3(param.getD1Operator3());
		m1.setOperator4(param.getD1Operator4());
		m1.setIsdelete(IsDeleteEnum.NO.getKey()+"");
		return m1;
	}
	
	/**
	 * 覆盖原有数据
	 * @param param
	 * @param m2
	 * @return
	 */
	public MonitorConfigDetail coverDetail2(UpdateMonitorConfigDetailReqParam param,MonitorConfigDetail m2) {
		m2.setConditionType(param.getD2ConditionType());
		m2.setOrgid(param.getOrgId());
		m2.setUpdatetime(LocalDateTime.now());
		if(StringUtils.isNotBlank(param.getD2MinValue1())) {
			m2.setMinValue1(Double.parseDouble(param.getD2MinValue1()));
		}else {
			m2.setMinValue1(null);
		}
		if(StringUtils.isNotBlank(param.getD2MinValue2())) {
			m2.setMinValue2(Double.parseDouble(param.getD2MinValue2()));	
		}else {
			m2.setMinValue2(null);
		}
		if(StringUtils.isNotBlank(param.getD2MaxValue1())) {
			m2.setMaxValue1(Double.parseDouble(param.getD2MaxValue1()));
		}else {
			m2.setMaxValue1(null);
		}
		if(StringUtils.isNotBlank(param.getD2MaxValue2())) {
			m2.setMaxValue2(Double.parseDouble(param.getD2MaxValue2()));
		}else {
			m2.setMaxValue2(null);
		}
		m2.setOperator1(param.getD2Operator1());
		m2.setOperator2(param.getD2Operator2());
		m2.setOperator3(param.getD2Operator3());
		m2.setOperator4(param.getD2Operator4());
		m2.setIsdelete(IsDeleteEnum.NO.getKey()+"");
		return m2;
	}
	
	/**
	 * 覆盖原有数据
	 * @param param
	 * @param m3
	 * @return
	 */
	public MonitorConfigDetail coverDetail3(UpdateMonitorConfigDetailReqParam param,MonitorConfigDetail m3) {
		m3.setConditionType(param.getD3ConditionType());
		m3.setOrgid(param.getOrgId());
		m3.setUpdatetime(LocalDateTime.now());
		if(StringUtils.isNotBlank(param.getD3MinValue1())) {
			m3.setMinValue1(Double.parseDouble(param.getD3MinValue1()));
		}else {
			m3.setMinValue1(null);
		}
		if(StringUtils.isNotBlank(param.getD3MinValue2())) {
			m3.setMinValue2(Double.parseDouble(param.getD3MinValue2()));	
		}else {
			m3.setMinValue2(null);
		}
		if(StringUtils.isNotBlank(param.getD3MaxValue1())) {
			m3.setMaxValue1(Double.parseDouble(param.getD3MaxValue1()));
		}else {
			m3.setMaxValue1(null);
		}
		if(StringUtils.isNotBlank(param.getD3MaxValue2())) {
			m3.setMaxValue2(Double.parseDouble(param.getD3MaxValue2()));
		}else {
			m3.setMaxValue2(null);
		}
		m3.setOperator1(param.getD3Operator1());
		m3.setOperator2(param.getD3Operator2());
		m3.setOperator3(param.getD3Operator3());
		m3.setOperator4(param.getD3Operator4());
		m3.setIsdelete(IsDeleteEnum.NO.getKey()+"");
		return m3;
	}

}
